
package com.huawei.workspace.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import com.huaweicloud.sdk.workspace.v2.WorkspaceClient;
import com.huaweicloud.sdk.workspace.v2.model.AdDomain;
import com.huaweicloud.sdk.workspace.v2.model.ApplyWorkspaceReq;
import com.huaweicloud.sdk.workspace.v2.model.ApplyWorkspaceRequest;
import com.huaweicloud.sdk.workspace.v2.model.ApplyWorkspaceResponse;
import com.huaweicloud.sdk.workspace.v2.model.CancelWorkspaceRequest;
import com.huaweicloud.sdk.workspace.v2.model.CancelWorkspaceResponse;
import com.huaweicloud.sdk.workspace.v2.model.ListWorkspacesRequest;
import com.huaweicloud.sdk.workspace.v2.model.ListWorkspacesResponse;
import com.huaweicloud.sdk.workspace.v2.model.ModifyWorkspaceAttributesReq;
import com.huaweicloud.sdk.workspace.v2.model.Subnet;
import com.huaweicloud.sdk.workspace.v2.model.UpdateWorkspaceRequest;
import com.huaweicloud.sdk.workspace.v2.model.UpdateWorkspaceResponse;
import com.huaweicloud.sdk.workspace.v2.region.WorkspaceRegion;

import java.util.ArrayList;
import java.util.List;

public class WorkspaceApplyWorkspacesDemo {

    public static void main(String[] args) {
        // 设置AK和SK
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";
        String projectId = "<YOUR PROJECTID>";

        ICredential auth = new BasicCredentials().withAk(ak).withSk(sk).withProjectId(projectId);

        HttpConfig config = HttpConfig.getDefaultHttpConfig();
        config.withIgnoreSSLVerification(true);
        WorkspaceClient client = WorkspaceClient.newBuilder()
            .withCredential(auth)
            .withRegion(WorkspaceRegion.valueOf("cn-north-4")) // 示例中区域为北京四，根据实际调用区域替换
            .withHttpConfig(config)
            .build();
        // 查询云办公服务详情
        listWorkspaces(client);
        // 开通云办公服务详情,不对接ad与云专线
        // applyWorkspace(client);
        // 修改云办公服务
        // updateWorkspace(client);
        // 注销云办公服务
        // cancelWorkspace(client);
    }

    private static void listWorkspaces(WorkspaceClient workspaceClient) {
        ListWorkspacesRequest request = new ListWorkspacesRequest();
        try {
            ListWorkspacesResponse response = workspaceClient.listWorkspaces(request);
            System.out.println(response.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    private static void applyWorkspace(WorkspaceClient workspaceClient) {
        String vpcId = "{your vpcId string}";
        String subnetId = "{your subnetId string}";
        List<Subnet> subnets = new ArrayList<>();
        subnets.add(new Subnet().withSubnetId(subnetId));
        try {
            ApplyWorkspaceReq applyWorkspaceReq = new ApplyWorkspaceReq();
            applyWorkspaceReq.withAccessMode(ApplyWorkspaceReq.AccessModeEnum.INTERNET)
                .withAdDomains(new AdDomain().withDomainType(AdDomain.DomainTypeEnum.LITE_AS))
                .withVpcId(vpcId)
                .withSubnetIds(subnets);
            ApplyWorkspaceResponse applyWorkspaceResponse =
                workspaceClient.applyWorkspace(new ApplyWorkspaceRequest().withBody(applyWorkspaceReq));
            System.out.println(applyWorkspaceResponse.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    private static void updateWorkspace(WorkspaceClient workspaceClient) {
        try {
            ModifyWorkspaceAttributesReq modifyWorkspaceAttributesReq = new ModifyWorkspaceAttributesReq();
            modifyWorkspaceAttributesReq.withAccessMode(ModifyWorkspaceAttributesReq.AccessModeEnum.BOTH);
            UpdateWorkspaceResponse updateWorkspaceResponse =
                workspaceClient.updateWorkspace(new UpdateWorkspaceRequest().withBody(modifyWorkspaceAttributesReq));
            System.out.println(updateWorkspaceResponse.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }

    private static void cancelWorkspace(WorkspaceClient workspaceClient) {
        try {
            CancelWorkspaceResponse cancelWorkspaceResponse =
                workspaceClient.cancelWorkspace(new CancelWorkspaceRequest());
            System.out.println(cancelWorkspaceResponse.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            System.out.println(e.getMessage());
        } catch (ServiceResponseException e) {
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}
